package lsl.leetCode.year2023.test0126;

import java.util.HashMap;

public class test169 {
    public static void main(String[] args) {
        int[] nums = {3, 2, 3};
        System.out.println(majorityElement(nums));
    }

    public static int majorityElement(int[] nums) {
        int length = nums.length;
        int val = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < length; i++) {
            int count = map.getOrDefault(nums[i], 0) + 1;
            map.put(nums[i], count);
            if (map.getOrDefault(nums[i], 0) > length / 2) {
                val = nums[i];
            }
        }
        return val;
    }
}
